package example.springboot.controller;

import example.springboot.dto.AccessTokenDTO;
import example.springboot.dto.GithubUser;
import example.springboot.mapper.userMapper;
import example.springboot.model.t_user;
import example.springboot.provider.GithubProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.UUID;

@Controller
public class AuthorizeController {
    @Autowired
    private GithubProvider githubProvider;
    @Autowired
    private userMapper userMapper;
    @Value("${github.Client_id}")
    private String Client_id;

    @Value("${github.Client_secret}")
    private String Client_secret;
    @Value("${github.Redirect_uri}")
    private String Redirect_uri;

    @GetMapping("/callback")
    public String callback(@RequestParam(name = "code") String code,
                           @RequestParam(name ="state") String state,
                           HttpServletResponse response){
        AccessTokenDTO accessToken = new AccessTokenDTO();
        accessToken.setCode(code);
        accessToken.setRedirect_uri(Redirect_uri);
        accessToken.setState(state);
        accessToken.setClient_id(Client_id);
        accessToken.setClient_secret(Client_secret);
        try {
            String accessToken1 = githubProvider.getAccessToken(accessToken);
            GithubUser githubUser = githubProvider.getUser(accessToken1);
            //通过固定userid查询用户
            t_user user = userMapper.byAccountidFind(String.valueOf(githubUser.getId()));
            String token;
            //如果用户为空则创建新用户存入数据库
            if (user==null){
                user=new t_user();
                user.setAccountid(String.valueOf(githubUser.getId()));
                user.setName(githubUser.getName());
                user.setAccountid(String.valueOf(githubUser.getId()));
                token = UUID.randomUUID().toString();
                user.setToken(token);
                user.setGmtcreate(System.currentTimeMillis());
                user.setGmtmodified(user.getGmtcreate());
                user.setAvatarurl(githubUser.getAvatarUrl());
                userMapper.insert(user);
            }else {
                //每次重新登陆更改token等属性
                token = UUID.randomUUID().toString();
                user.setToken(token);
                user.setGmtcreate(System.currentTimeMillis());
                user.setGmtmodified(user.getGmtcreate());
                user.setAvatarurl(githubUser.getAvatarUrl());
                userMapper.byIdUpdate(user.getToken(), user.getGmtcreate(), user.getGmtmodified());
            }
            //token验证
            response.addCookie(new Cookie("token",token));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/index1";
    }
    @GetMapping("/logout")
    public String logout(HttpServletRequest request,
                         HttpServletResponse response) {
        request.getSession().removeAttribute("t_user");
        Cookie cookie = new Cookie("token", null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "redirect:/";
    }
}
